using System;
using System.Collections.Generic;
using System.Text;
using Example.Engine.PluginBase;

using Example.Engine;
using Example.Plugin1.Xml;
using DynamiteXml;

namespace Example.Plugin1
{
    class Plugin1 : BasePlugin
    {
        public override void ModifyRecordWithDescendentXml(ItemWithXmlDS.ItemWithXmlRow row)
        {
            DescendentXml xml;

            // Get the xml . An invalid cast to the generic type will cause an exception.
            xml = DynamiteXmlLogic.Deserialize<DescendentXml>(row.Xml);
            // Or, to avoid a null reference exception: 
            xml = DynamiteXmlLogic.Deserialize<DescendentXml>(row.Xml, true);
            // Or, to cast manually:
            xml = DynamiteXmlLogic.Deserialize(row.Xml) as DescendentXml;

            xml.ParentProperty++;
            xml.DescendentProperty = "Overridden : " + xml.DescendentProperty;

            row.Xml = DynamiteXmlLogic.Serialize(xml);
        }

        public override void ModifyRecordWithParentXml(ItemWithXmlDS.ItemWithXmlRow row)
        {
            ParentXml xml;

            // Getting/casting the xml  below using type 'DescendentXml' will cause an exception 
            // at the first statement and a null value in the 2nd & third.

            // Get the xml . An invalid cast to the generic type will cause an exception.
            xml = DynamiteXmlLogic.Deserialize<ParentXml>(row.Xml);
            // Or, to avoid a null reference exception: 
            xml = DynamiteXmlLogic.Deserialize<ParentXml>(row.Xml, true);
            // Or, to cast manually:
            xml = DynamiteXmlLogic.Deserialize(row.Xml) as ParentXml;


            xml.ParentProperty++;

            /*  Code below cannot be executed, the xml could not be casted to DescendentXml
            xml.DescendentProperty = "Overridden : " + xml.DescendentProperty;
            */

            row.Xml = DynamiteXmlLogic.Serialize(xml);
        }
    }
}
